home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / mozilla-firefox / include / js / jsparse.h < prev    next >
C/C++ Source or Header  |  2006-05-08  |  21KB  |  415 lines

  1. /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  *
  3.  * ***** BEGIN LICENSE BLOCK *****
  4.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  5.  *
  6.  * The contents of this file are subject to the Mozilla Public License Version
  7.  * 1.1 (the "License"); you may not use this file except in compliance with
  8.  * the License. You may obtain a copy of the License at
  9.  * http://www.mozilla.org/MPL/
  10.  *
  11.  * Software distributed under the License is distributed on an "AS IS" basis,
  12.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13.  * for the specific language governing rights and limitations under the
  14.  * License.
  15.  *
  16.  * The Original Code is Mozilla Communicator client code, released
  17.  * March 31, 1998.
  18.  *
  19.  * The Initial Developer of the Original Code is
  20.  * Netscape Communications Corporation.
  21.  * Portions created by the Initial Developer are Copyright (C) 1998
  22.  * the Initial Developer. All Rights Reserved.
  23.  *
  24.  * Contributor(s):
  25.  *
  26.  * Alternatively, the contents of this file may be used under the terms of
  27.  * either of the GNU General Public License Version 2 or later (the "GPL"),
  28.  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  29.  * in which case the provisions of the GPL or the LGPL are applicable instead
  30.  * of those above. If you wish to allow use of your version of this file only
  31.  * under the terms of either the GPL or the LGPL, and not to allow others to
  32.  * use your version of this file under the terms of the MPL, indicate your
  33.  * decision by deleting the provisions above and replace them with the notice
  34.  * and other provisions required by the GPL or the LGPL. If you do not delete
  35.  * the provisions above, a recipient may use your version of this file under
  36.  * the terms of any one of the MPL, the GPL or the LGPL.
  37.  *
  38.  * ***** END LICENSE BLOCK ***** */
  39.  
  40. #ifndef jsparse_h___
  41. #define jsparse_h___
  42. /*
  43.  * JS parser definitions.
  44.  */
  45. #include "jsconfig.h"
  46. #include "jsprvtd.h"
  47. #include "jspubtd.h"
  48. #include "jsscan.h"
  49.  
  50. JS_BEGIN_EXTERN_C
  51.  
  52. /*
  53.  * Parsing builds a tree of nodes that directs code generation.  This tree is
  54.  * not a concrete syntax tree in all respects (for example, || and && are left
  55.  * associative, but (A && B && C) translates into the right-associated tree
  56.  * <A && <B && C>> so that code generation can emit a left-associative branch
  57.  * around <B && C> when A is false).  Nodes are labeled by token type, with a
  58.  * JSOp secondary label when needed:
  59.  *
  60.  * Label        Variant     Members
  61.  * -----        -------     -------
  62.  * <Definitions>
  63.  * TOK_FUNCTION func        pn_funAtom: atom holding function object containing
  64.  *                            arg and var properties.  We create the function
  65.  *                            object at parse (not emit) time to specialize arg
  66.  *                            and var bytecodes early.
  67.  *                          pn_body: TOK_LC node for function body statements
  68.  *                          pn_flags: TCF_FUN_* flags (see jsemit.h) collected
  69.  *                            while parsing the function's body
  70.  *                          pn_tryCount: of try statements in function
  71.  *
  72.  * <Statements>
  73.  * TOK_LC       list        pn_head: list of pn_count statements
  74.  * TOK_EXPORT   list        pn_head: list of pn_count TOK_NAMEs or one TOK_STAR
  75.  *                            (which is not a multiply node)
  76.  * TOK_IMPORT   list        pn_head: list of pn_count sub-trees of the form
  77.  *                            a.b.*, a[b].*, a.*, a.b, or a[b] -- but never a.
  78.  *                            Each member is expressed with TOK_DOT or TOK_LB.
  79.  *                            Each sub-tree's root node has a pn_op in the set
  80.  *                            JSOP_IMPORT{ALL,PROP,ELEM}
  81.  * TOK_IF       ternary     pn_kid1: cond, pn_kid2: then, pn_kid3: else or null
  82.  * TOK_SWITCH   binary      pn_left: discriminant
  83.  *                          pn_right: list of TOK_CASE nodes, with at most one
  84.  *                            TOK_DEFAULT node
  85.  * TOK_CASE,    binary      pn_left: case expr or null if TOK_DEFAULT
  86.  * TOK_DEFAULT              pn_right: TOK_LC node for this case's statements
  87.  *                          pn_val: constant value if lookup or table switch
  88.  * TOK_WHILE    binary      pn_left: cond, pn_right: body
  89.  * TOK_DO       binary      pn_left: body, pn_right: cond
  90.  * TOK_FOR      binary      pn_left: either
  91.  *                            for/in loop: a binary TOK_IN node with
  92.  *                              pn_left:  TOK_VAR or TOK_NAME to left of 'in'
  93.  *                                if TOK_VAR, its pn_extra may have PNX_POPVAR
  94.  *                                and PNX_FORINVAR bits set
  95.  *                              pn_right: object expr to right of 'in'
  96.  *                            for(;;) loop: a ternary TOK_RESERVED node with
  97.  *                              pn_kid1:  init expr before first ';'
  98.  *                              pn_kid2:  cond expr before second ';'
  99.  *                              pn_kid3:  update expr after second ';'
  100.  *                              any kid may be null
  101.  *                          pn_right: body
  102.  * TOK_THROW    unary       pn_op: JSOP_THROW, pn_kid: exception
  103.  * TOK_TRY      ternary     pn_kid1: try block
  104.  *                          pn_kid2: catch blocks or null
  105.  *                          pn_kid3: finally block or null
  106.  * TOK_CATCH    ternary     pn_kid1: PN_NAME node for catch var (with pn_expr
  107.  *                                   null or the catch guard expression)
  108.  *                          pn_kid2: more catch blocks or null
  109.  *                          pn_kid3: catch block statements
  110.  * TOK_BREAK    name        pn_atom: label or null
  111.  * TOK_CONTINUE name        pn_atom: label or null
  112.  * TOK_WITH     binary      pn_left: head expr, pn_right: body
  113.  * TOK_VAR      list        pn_head: list of pn_count TOK_NAME nodes
  114.  *                                   each name node has
  115.  *                                     pn_atom: variable name
  116.  *                                     pn_expr: initializer or null
  117.  * TOK_RETURN   unary       pn_kid: return expr or null
  118.  * TOK_SEMI     unary       pn_kid: expr or null statement
  119.  * TOK_COLON    name        pn_atom: label, pn_expr: labeled statement
  120.  *
  121.  * <Expressions>
  122.  * All left-associated binary trees of the same type are optimized into lists
  123.  * to avoid recursion when processing expression chains.
  124.  * TOK_COMMA    list        pn_head: list of pn_count comma-separated exprs
  125.  * TOK_ASSIGN   binary      pn_left: lvalue, pn_right: rvalue
  126.  *                          pn_op: JSOP_ADD for +=, etc.
  127.  * TOK_HOOK     ternary     pn_kid1: cond, pn_kid2: then, pn_kid3: else
  128.  * TOK_OR       binary      pn_left: first in || chain, pn_right: rest of chain
  129.  * TOK_AND      binary      pn_left: first in && chain, pn_right: rest of chain
  130.  * TOK_BITOR    binary      pn_left: left-assoc | expr, pn_right: ^ expr
  131.  * TOK_BITXOR   binary      pn_left: left-assoc ^ expr, pn_right: & expr
  132.  * TOK_BITAND   binary      pn_left: left-assoc & expr, pn_right: EQ expr
  133.  * TOK_EQOP     binary      pn_left: left-assoc EQ expr, pn_right: REL expr
  134.  *                          pn_op: JSOP_EQ, JSOP_NE, JSOP_NEW_EQ, JSOP_NEW_NE
  135.  * TOK_RELOP    binary      pn_left: left-assoc REL expr, pn_right: SH expr
  136.  *                          pn_op: JSOP_LT, JSOP_LE, JSOP_GT, JSOP_GE
  137.  * TOK_SHOP     binary      pn_left: left-assoc SH expr, pn_right: ADD expr
  138.  *                          pn_op: JSOP_LSH, JSOP_RSH, JSOP_URSH
  139.  * TOK_PLUS,    binary      pn_left: left-assoc ADD expr, pn_right: MUL expr
  140.  *                          pn_extra: if a left-associated binary TOK_PLUS
  141.  *                            tree has been flattened into a list (see above
  142.  *                            under <Expressions>), pn_extra will contain
  143.  *                            PNX_STRCAT if at least one list element is a
  144.  *                            string literal (TOK_STRING); if such a list has
  145.  *                            any non-string, non-number term, pn_extra will
  146.  *                            contain PNX_CANTFOLD.
  147.  *                          pn_
  148.  * TOK_MINUS                pn_op: JSOP_ADD, JSOP_SUB
  149.  * TOK_STAR,    binary      pn_left: left-assoc MUL expr, pn_right: UNARY expr
  150.  * TOK_DIVOP                pn_op: JSOP_MUL, JSOP_DIV, JSOP_MOD
  151.  * TOK_UNARYOP  unary       pn_kid: UNARY expr, pn_op: JSOP_NEG, JSOP_POS,
  152.  *                          JSOP_NOT, JSOP_BITNOT, JSOP_TYPEOF, JSOP_VOID
  153.  * TOK_INC,     unary       pn_kid: MEMBER expr
  154.  * TOK_DEC
  155.  * TOK_NEW      list        pn_head: list of ctor, arg1, arg2, ... argN
  156.  *                          pn_count: 1 + N (where N is number of args)
  157.  *                          ctor is a MEMBER expr
  158.  * TOK_DELETE   unary       pn_kid: MEMBER expr
  159.  * TOK_DOT,     name        pn_expr: MEMBER expr to left of .
  160.  * TOK_DBLDOT               pn_atom: name to right of .
  161.  * TOK_LB       binary      pn_left: MEMBER expr to left of [
  162.  *                          pn_right: expr between [ and ]
  163.  * TOK_LP       list        pn_head: list of call, arg1, arg2, ... argN
  164.  *                          pn_count: 1 + N (where N is number of args)
  165.  *                          call is a MEMBER expr naming a callable object
  166.  * TOK_RB       list        pn_head: list of pn_count array element exprs
  167.  *                          [,,] holes are represented by TOK_COMMA nodes
  168.  *                          #n=[...] produces TOK_DEFSHARP at head of list
  169.  *                          pn_extra: PN_ENDCOMMA if extra comma at end
  170.  * TOK_RC       list        pn_head: list of pn_count TOK_COLON nodes where
  171.  *                          each has pn_left: property id, pn_right: value
  172.  *                          #n={...} produces TOK_DEFSHARP at head of list
  173.  * TOK_DEFSHARP unary       pn_num: jsint value of n in #n=
  174.  *                          pn_kid: null for #n=[...] and #n={...}, primary
  175.  *                          if #n=primary for function, paren, name, object
  176.  *                          literal expressions
  177.  * TOK_USESHARP nullary     pn_num: jsint value of n in #n#
  178.  * TOK_RP       unary       pn_kid: parenthesized expression
  179.  * TOK_NAME,    name        pn_atom: name, string, or object atom
  180.  * TOK_STRING,              pn_op: JSOP_NAME, JSOP_STRING, or JSOP_OBJECT, or
  181.  *                                 JSOP_REGEXP
  182.  * TOK_OBJECT               If JSOP_NAME, pn_op may be JSOP_*ARG or JSOP_*VAR
  183.  *                          with pn_slot >= 0 and pn_attrs telling const-ness
  184.  * TOK_NUMBER   dval        pn_dval: double value of numeric literal
  185.  * TOK_PRIMARY  nullary     pn_op: JSOp bytecode
  186.  * TOK_ANYNAME  nullary     pn_op: JSOP_ANYNAME
  187.  *                          pn_atom: cx->runtime->atomState.starAtom
  188.  * TOK_AT       unary       pn_op: JSOP_TOATTRNAME; pn_kid attribute id/expr
  189.  * TOK_DBLCOLON binary      pn_op: JSOP_QNAME
  190.  *                          pn_left: TOK_ANYNAME or TOK_NAME node
  191.  *                          pn_right: TOK_STRING "*" node, or expr within []
  192.  *              name        pn_op: JSOP_QNAMECONST
  193.  *                          pn_expr: TOK_ANYNAME or TOK_NAME left operand
  194.  *                          pn_atom: name on right of ::
  195.  * TOK_XMLELEM  list        XML element node
  196.  *                          pn_head: start tag, content1, ... contentN, end tag
  197.  *                          pn_count: 2 + N where N is number of content nodes
  198.  *                                    N may be > x.length() if {expr} embedded
  199.  * TOK_XMLLIST  list        XML list node
  200.  *                          pn_head: content1, ... contentN
  201.  * TOK_XMLSTAGO, list       XML start, end, and point tag contents
  202.  * TOK_XMLETAGC,            pn_head: tag name or {expr}, ... XML attrs ...
  203.  * TOK_XMLPTAGO
  204.  * TOK_XMLNAME  nullary     pn_atom: XML name, with no {expr} embedded
  205.  * TOK_XMLNAME  list        pn_head: tag name or {expr}, ... name or {expr}
  206.  * TOK_XMLATTR, nullary     pn_atom: attribute value string; pn_op: JSOP_STRING
  207.  * TOK_XMLCDATA,
  208.  * TOK_XMLCOMMENT
  209.  * TOK_XMLPI    nullary     pn_atom: XML processing instruction target
  210.  *                          pn_atom2: XML PI content, or null if no content
  211.  * TOK_XMLTEXT  nullary     pn_atom: marked-up text, or null if empty string
  212.  * TOK_LC       unary       {expr} in XML tag or content; pn_kid is expr
  213.  *
  214.  * So an XML tag with no {expr} and three attributes is a list with the form:
  215.  *
  216.  *    (tagname attrname1 attrvalue1 attrname2 attrvalue2 attrname2 attrvalue3)
  217.  *
  218.  * An XML tag with embedded expressions like so:
  219.  *
  220.  *    <name1{expr1} name2{expr2}name3={expr3}>
  221.  *
  222.  * would have the form:
  223.  *
  224.  *    ((name1 {expr1}) (name2 {expr2} name3) {expr3})
  225.  *
  226.  * where () bracket a list with elements separated by spaces, and {expr} is a
  227.  * TOK_LC unary node with expr as its kid.
  228.  *
  229.  * Thus, the attribute name/value pairs occupy successive odd and even list
  230.  * locations, where pn_head is the TOK_XMLNAME node at list location 0.  The
  231.  * parser builds the same sort of structures for elements:
  232.  *
  233.  *    <a x={x}>Hi there!<b y={y}>How are you?</b><answer>{x + y}</answer></a>
  234.  *
  235.  * translates to:
  236.  *
  237.  *    ((a x {x}) 'Hi there!' ((b y {y}) 'How are you?') ((answer) {x + y}))
  238.  */
  239. typedef enum JSParseNodeArity {
  240.     PN_FUNC     = -3,
  241.     PN_LIST     = -2,
  242.     PN_TERNARY  =  3,
  243.     PN_BINARY   =  2,
  244.     PN_UNARY    =  1,
  245.     PN_NAME     = -1,
  246.     PN_NULLARY  =  0
  247. } JSParseNodeArity;
  248.  
  249. struct JSParseNode {
  250.     uint16              pn_type;
  251.     uint8               pn_op;
  252.     int8                pn_arity;
  253.     JSTokenPos          pn_pos;
  254.     ptrdiff_t           pn_offset;      /* first generated bytecode offset */
  255.     union {
  256.         struct {                        /* TOK_FUNCTION node */
  257.             JSAtom      *funAtom;       /* atomized function object */
  258.             JSParseNode *body;          /* TOK_LC list of statements */
  259.             uint32      flags;          /* accumulated tree context flags */
  260.             uint32      tryCount;       /* count of try statements in body */
  261.         } func;
  262.         struct {                        /* list of next-linked nodes */
  263.             JSParseNode *head;          /* first node in list */
  264.             JSParseNode **tail;         /* ptr to ptr to last node in list */
  265.             uint32      count;          /* number of nodes in list */
  266.             uint32      extra;          /* extra comma flag for [1,2,,] */
  267.         } list;
  268.         struct {                        /* ternary: if, for(;;), ?: */
  269.             JSParseNode *kid1;          /* condition, discriminant, etc. */
  270.             JSParseNode *kid2;          /* then-part, case list, etc. */
  271.             JSParseNode *kid3;          /* else-part, default case, etc. */
  272.         } ternary;
  273.         struct {                        /* two kids if binary */
  274.             JSParseNode *left;
  275.             JSParseNode *right;
  276.             jsval       val;            /* switch case value */
  277.         } binary;
  278.         struct {                        /* one kid if unary */
  279.             JSParseNode *kid;
  280.             jsint       num;            /* -1 or sharp variable number */
  281.         } unary;
  282.         struct {                        /* name, labeled statement, etc. */
  283.             JSAtom      *atom;          /* name or label atom, null if slot */
  284.             JSParseNode *expr;          /* object or initializer */
  285.             jsint       slot;           /* -1 or arg or local var slot */
  286.             uintN       attrs;          /* attributes if local var or const */
  287.         } name;
  288.         struct {
  289.             JSAtom      *atom;          /* first atom in pair */
  290.             JSAtom      *atom2;         /* second atom in pair or null */
  291.         } apair;
  292.         jsdouble        dval;           /* aligned numeric literal value */
  293.     } pn_u;
  294.     JSParseNode         *pn_next;       /* to align dval and pn_u on RISCs */
  295. #if JS_HAS_XML_SUPPORT
  296.     JSTokenStream       *pn_ts;         /* token stream for XML error reports */
  297. #endif
  298. };
  299.  
  300. #define pn_funAtom      pn_u.func.funAtom
  301. #define pn_body         pn_u.func.body
  302. #define pn_flags        pn_u.func.flags
  303. #define pn_tryCount     pn_u.func.tryCount
  304. #define pn_head         pn_u.list.head
  305. #define pn_tail         pn_u.list.tail
  306. #define pn_count        pn_u.list.count
  307. #define pn_extra        pn_u.list.extra
  308. #define pn_kid1         pn_u.ternary.kid1
  309. #define pn_kid2         pn_u.ternary.kid2
  310. #define pn_kid3         pn_u.ternary.kid3
  311. #define pn_left         pn_u.binary.left
  312. #define pn_right        pn_u.binary.right
  313. #define pn_val          pn_u.binary.val
  314. #define pn_kid          pn_u.unary.kid
  315. #define pn_num          pn_u.unary.num
  316. #define pn_atom         pn_u.name.atom
  317. #define pn_expr         pn_u.name.expr
  318. #define pn_slot         pn_u.name.slot
  319. #define pn_attrs        pn_u.name.attrs
  320. #define pn_dval         pn_u.dval
  321. #define pn_atom2        pn_u.apair.atom2
  322.  
  323. /* PN_LIST pn_extra flags. */
  324. #define PNX_STRCAT      0x01            /* TOK_PLUS list has string term */
  325. #define PNX_CANTFOLD    0x02            /* TOK_PLUS list has unfoldable term */
  326. #define PNX_POPVAR      0x04            /* TOK_VAR last result needs popping */
  327. #define PNX_FORINVAR    0x08            /* TOK_VAR is left kid of TOK_IN node,
  328.                                            which is left kid of TOK_FOR */
  329. #define PNX_ENDCOMMA    0x10            /* array literal has comma at end */
  330. #define PNX_XMLROOT     0x20            /* top-most node in XML literal tree */
  331.  
  332. /*
  333.  * Move pn2 into pn, preserving pn->pn_pos and pn->pn_offset and handing off
  334.  * any kids in pn2->pn_u, by clearing pn2.
  335.  */
  336. #define PN_MOVE_NODE(pn, pn2)                                                 \
  337.     JS_BEGIN_MACRO                                                            \
  338.         (pn)->pn_type = (pn2)->pn_type;                                       \
  339.         (pn)->pn_op = (pn2)->pn_op;                                           \
  340.         (pn)->pn_arity = (pn2)->pn_arity;                                     \
  341.         (pn)->pn_u = (pn2)->pn_u;                                             \
  342.         PN_CLEAR_NODE(pn2);                                                   \
  343.     JS_END_MACRO
  344.  
  345. #define PN_CLEAR_NODE(pn)                                                     \
  346.     JS_BEGIN_MACRO                                                            \
  347.         (pn)->pn_type = TOK_EOF;                                              \
  348.         (pn)->pn_op = JSOP_NOP;                                               \
  349.         (pn)->pn_arity = PN_NULLARY;                                          \
  350.     JS_END_MACRO
  351.  
  352. /* True if pn is a parsenode representing a literal constant. */
  353. #define PN_IS_CONSTANT(pn)                                                    \
  354.     ((pn)->pn_type == TOK_NUMBER ||                                           \
  355.      (pn)->pn_type == TOK_STRING ||                                           \
  356.      ((pn)->pn_type == TOK_PRIMARY && (pn)->pn_op != JSOP_THIS))
  357.  
  358. /*
  359.  * Compute a pointer to the last JSParseNode element in a singly-linked list.
  360.  * NB: list must be non-empty for correct PN_LAST usage!
  361.  */
  362. #define PN_LAST(list) \
  363.     ((JSParseNode *)((char *)(list)->pn_tail - offsetof(JSParseNode, pn_next)))
  364.  
  365. #define PN_INIT_LIST(list)                                                    \
  366.     JS_BEGIN_MACRO                                                            \
  367.         (list)->pn_head = NULL;                                               \
  368.         (list)->pn_tail = &(list)->pn_head;                                   \
  369.         (list)->pn_count = (list)->pn_extra = 0;                              \
  370.     JS_END_MACRO
  371.  
  372. #define PN_INIT_LIST_1(list, pn)                                              \
  373.     JS_BEGIN_MACRO                                                            \
  374.         (list)->pn_head = (pn);                                               \
  375.         (list)->pn_tail = &(pn)->pn_next;                                     \
  376.         (list)->pn_count = 1;                                                 \
  377.         (list)->pn_extra = 0;                                                 \
  378.     JS_END_MACRO
  379.  
  380. #define PN_APPEND(list, pn)                                                   \
  381.     JS_BEGIN_MACRO                                                            \
  382.         *(list)->pn_tail = (pn);                                              \
  383.         (list)->pn_tail = &(pn)->pn_next;                                     \
  384.         (list)->pn_count++;                                                   \
  385.     JS_END_MACRO
  386.  
  387. /*
  388.  * Parse a top-level JS script.
  389.  *
  390.  * The caller must prevent the GC from running while this function is active,
  391.  * because atoms and function newborns are not rooted yet.
  392.  */
  393. extern JS_FRIEND_API(JSParseNode *)
  394. js_ParseTokenStream(JSContext *cx, JSObject *chain, JSTokenStream *ts);
  395.  
  396. extern JS_FRIEND_API(JSBool)
  397. js_CompileTokenStream(JSContext *cx, JSObject *chain, JSTokenStream *ts,
  398.                       JSCodeGenerator *cg);
  399.  
  400. extern JSBool
  401. js_CompileFunctionBody(JSContext *cx, JSTokenStream *ts, JSFunction *fun);
  402.  
  403. extern JSBool
  404. js_FoldConstants(JSContext *cx, JSParseNode *pn, JSTreeContext *tc);
  405.  
  406. #if JS_HAS_XML_SUPPORT
  407. JS_FRIEND_API(JSParseNode *)
  408. js_ParseXMLTokenStream(JSContext *cx, JSObject *chain, JSTokenStream *ts,
  409.                        JSBool allowList);
  410. #endif
  411.  
  412. JS_END_EXTERN_C
  413.  
  414. #endif /* jsparse_h___ */
  415.